package sheep.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JPanel;


import org.jdesktop.fuse.InjectedResource;
import org.jdesktop.fuse.ResourceInjector;
import org.jdesktop.swingx.JXPanel;
import org.jdesktop.swingx.StackLayout;
import sheep.controller.Workspace;
import sheep.model.Task.GraphMode;
import sheep.view.Component.ActionButton;
import sheep.view.Component.RoundedPanel;
import sheep.view.VisualGanttPanel.GantMode;
import sheep.view.transitions.ScreenTransition;
import sheep.view.transitions.TransitionTarget;

public class VisualProjectPanel extends JXPanel {

    private VisualGraphEditPanel editPanel;
    private VisualGanttPanel editPanel2;
    private VisualGanttPanel editPanel3;
    private JPanel centerPanel;
    ////////////////////////////////////////////////////////////////////////////
    // THEME SPECIFIC FIELDS
    ////////////////////////////////////////////////////////////////////////////
    /** @noinspection UNUSED_SYMBOL*/
    @InjectedResource
    private Color websiteBackground;
    private Workspace workSpace;

    ActionButton normalModeButton;
    ActionButton ganttButton;
    
    // Flag pour connaitre le panel affich"
  public enum GrpaheScreenType { GRAPH,GANTT, GANTT_EXTEND }
  // Valeur des ecrans courant et precedent
  public GrpaheScreenType currentScreen;
  public GrpaheScreenType oldScreen;   
  
  // Gestionnaire de transitions utilisee    
  private ScreenTransition screenTransition; 
  
  private static TaskGraphDlg taskGraphDlg;
  
  private static final int TRANSITION_TIME_IN_MILLIS = 500;  
      
    public GrpaheScreenType getCurrentScreen() {
    return currentScreen;
  }

  public void setCurrentScreen(GrpaheScreenType currentScreen) {
    this.currentScreen = currentScreen;
  }
  
  /////////////////////////////////////////////////////////////////////////

  private class ContentTransitionTarget implements TransitionTarget {

  /////////////////////////////////////////////////////////////////////////
   

  
  
  @Override
  // Vide la zone principal
  public void resetCurrentScreen() {
     centerPanel.removeAll();
  }

  @Override
  // Ajoute le panel correspondant au flag au panel general
  public void setupNextScreen() {
     switch(getCurrentScreen()) {
        case GRAPH:
             centerPanel.add(editPanel, BorderLayout.CENTER);
             break;
        case GANTT:
             centerPanel.add(editPanel2, BorderLayout.CENTER);
             break; 
         case GANTT_EXTEND:
             centerPanel.add(editPanel3, BorderLayout.CENTER);
             break;             
        default:
              assert false;
              break;
     }    
  }
      
  @Override
  // Si on vient du slideshow, on le detruit
  public void transitionComplete() {}
 
  }
      
    // Lance la trnsition vers le nouveau panel
  private void startTransition(GrpaheScreenType newScreen) {
     if (newScreen != getCurrentScreen()) {
         oldScreen = getCurrentScreen();
         setCurrentScreen(newScreen);
         screenTransition.startTransition(TRANSITION_TIME_IN_MILLIS);
     }
  }
  
    // Affiche la panel principal
  public void showGraphe() {  
     startTransition(GrpaheScreenType.GRAPH);
     setCurrentScreen(GrpaheScreenType.GRAPH);
  }
    
  // Affiche la panel de projet
  public void showGANTT() {       

     startTransition(GrpaheScreenType.GANTT);
     setCurrentScreen(GrpaheScreenType.GANTT);
  }
    
  // Affiche la panel de projet
  public void showGanttExtend() {       

     startTransition(GrpaheScreenType.GANTT_EXTEND);
     setCurrentScreen(GrpaheScreenType.GANTT_EXTEND);
  }
  
  
  
    public VisualProjectPanel(Workspace workSpace) {        
        ResourceInjector.get().inject(this);
        this.workSpace=workSpace;
        
        taskGraphDlg = new TaskGraphDlg(workSpace);
            
        setOpaque(false);
        setLayout(new BorderLayout());

        ActionButton previewButton = new ActionButton("Sauver");
        previewButton.setMain(true);
        
        ActionButton grapheButton = new ActionButton("Graphe");
        grapheButton.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            showGraphe();
          }
        });
        
        ganttButton = new ActionButton("Gantt");
        ganttButton.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            if (ganttButton.getText().equals("Gantt")){
              showGANTT();
              ganttButton.setText("Gantt extend");
            }else{
              showGanttExtend();
              ganttButton.setText("Gantt");
            }
          }
        });
        
        
        
        
        normalModeButton = new ActionButton("Normal");
        normalModeButton.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            if (normalModeButton.getText().equals("Normal")){
              VisualProjectPanel.this.workSpace.getCurrentTask().setGraphMode(GraphMode.NORMAL);
              VisualProjectPanel.this.refreshGraph();
              normalModeButton.setText("AutoArranged");
            }else{
              VisualProjectPanel.this.workSpace.getCurrentTask().setGraphMode(GraphMode.AUTOARRANGED);
              VisualProjectPanel.this.workSpace.getCurrentTask().autoArranged();
//            VisualProjectPanel.this.workSpace.getCurrentProject().setGraphMode(GraphMode.AUTOARRANGED);
              VisualProjectPanel.this.refreshGraph();
              normalModeButton.setText("Normal");
            }
            
            
          }
        });
  
        JPanel temp1 = new JPanel();
        temp1.setOpaque(false);
        temp1.setPreferredSize(new Dimension(150, 50));
        temp1.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 15));
        temp1.setLayout(new BorderLayout());
        temp1.add(previewButton, BorderLayout.EAST);
        
        JPanel temp2 = new JPanel();
        temp2.setOpaque(false);
        temp2.setPreferredSize(new Dimension(150, 50));
        temp2.setLayout(new BorderLayout());
        temp2.add(Box.createHorizontalStrut(150), BorderLayout.EAST);
        

        JPanel buttonPanel = new JPanel();
        buttonPanel.setOpaque(false);
        buttonPanel.add(grapheButton);
        buttonPanel.add(ganttButton);
        buttonPanel.add(normalModeButton);

        JPanel temp3 = new JPanel();
        temp3.setOpaque(false);
        temp3.setPreferredSize(new Dimension(150, 50));
        temp3.setLayout(new BorderLayout());
        temp3.add(temp1, BorderLayout.EAST);
        temp3.add(temp2, BorderLayout.WEST);
        temp3.add(buttonPanel, BorderLayout.CENTER);
        
        temp3.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
        add(temp3,BorderLayout.SOUTH);
        editPanel = new VisualGraphEditPanel(workSpace);
        editPanel2= new VisualGanttPanel(workSpace, GantMode.NORMAL);
        editPanel3= new VisualGanttPanel(workSpace, GantMode.EXTEND);
        centerPanel = new JPanel();
        centerPanel.setOpaque(false);
        centerPanel.setLayout(new BorderLayout());
        centerPanel.add(editPanel, BorderLayout.CENTER);
        
        JXPanel centerSatckPanel = new JXPanel(new StackLayout());
        centerSatckPanel.setOpaque(false);
        centerSatckPanel. add(new RoundedPanel(centerPanel), StackLayout.TOP);
        taskGraphDlg.setVisible2(false);
        taskGraphDlg.setBorder(javax.swing.BorderFactory.createEmptyBorder(20, 375, 20, 375));
        centerSatckPanel.add(taskGraphDlg,StackLayout.TOP );    
 
        add(BorderLayout.CENTER, centerSatckPanel);
        
        add(BorderLayout.WEST, Box.createHorizontalStrut(20));
        add(BorderLayout.NORTH, Box.createVerticalStrut(20));
        add(BorderLayout.EAST, Box.createHorizontalStrut(20));      
        

    
        screenTransition = new ScreenTransition(centerPanel, 
        new ContentTransitionTarget());
        refreshGraph();
    }
   
    public static void showDlg(){
      taskGraphDlg.setVisible(!taskGraphDlg.isVisible());
    }
    
    public void refreshGraph(){
     if (this.workSpace.getCurrentTask()!=null){
        editPanel.refreshGraph();
        repaint();
     }
   }
}
